package cn.wp.product.controller;

import cn.wp.basic.util.AjaxResult;
import cn.wp.basic.util.PageList;
import cn.wp.product.domain.Product;
import cn.wp.product.query.ProductQuery;
import cn.wp.product.service.IProductService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    /**
     * 分页高级查询列表显示
     * @param productQuery
     * @return
     */
    @PostMapping
    public PageList<Product> queryPage(@RequestBody ProductQuery productQuery){
        return productService.queryPage(productQuery);
    }

    @PostMapping("/list")
    public PageList<Product> queryPageList(@RequestBody ProductQuery productQuery){
        productQuery.setState(1);
        return productService.queryPage(productQuery);
    }

    /**
     * 新增和修改
     * @param department
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Product product){
        try {
            if (product.getId() == null){
                productService.add(product);
            } else {
                productService.update(product);
            }
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败！");
        }
    }

    /**
     * 批量上架
     * @param ids
     * @return
     */
    @PatchMapping("/onSale")
    public AjaxResult onSale(@RequestBody List<Long> ids){
        try {
            productService.onSale(ids);
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("上架失败！");
        }
    }

    /**
     * 批量下架
     * @param ids
     * @return
     */
    @PatchMapping("/offSale")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        try {
            productService.offSale(ids);
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("下架失败！");
        }
    }

    /**
     * 删除一个对象
     * @param id
     * @return
     */
    @ApiOperation(value = "删除一个门店对象",notes = "根据id删除")
    @DeleteMapping("/{id}")
    public AjaxResult delect(@PathVariable("id") Long id){
        try {
            productService.delete(id);
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败！");
        }
    }

    /**
     * 产品详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getProductById(@PathVariable("id") Long id){
        Product product = productService.findOne(id);
        return AjaxResult.me().setResultObject(product);
    }
}
